﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using Entity = VietStar.Library.Entity;
using Business = VietStar.Library.Business;

public partial class control_client_mainmenu : System.Web.UI.UserControl
{
    DataTable dsDanhMucCha = new DataTable();
    DataTable dsDanhMucCon1 = new DataTable();
    Business.ChuyenMuc business = new Business.ChuyenMuc();
    Entity.ChuyenMuc entity = new Entity.ChuyenMuc();   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDanhMuc();
        }
    }
    protected void LoadDanhMuc()
    {
        try
        {
            string str = "";
            
            dsDanhMucCha = business.SelectDanhMucCha();
            if (dsDanhMucCha.Rows.Count > 0)
            {
                str += "<ul id=\"default_364\" class=\"default_horiz_dropdown_mm level0\">";
                for (int i = 0; i < dsDanhMucCha.Rows.Count; i++)
                {
                    if (Convert.ToBoolean(dsDanhMucCha.Rows[i]["Tren"].ToString()) == true && Convert.ToBoolean(dsDanhMucCha.Rows[i]["TrangThai"].ToString()) == true)
                    {
                        entity.DanhMucCha = Convert.ToInt32(dsDanhMucCha.Rows[i]["Id"].ToString());
                        dsDanhMucCon1 = business.SelectChuyenMucCon(entity);
                        string link = "";
                        string css = "";
                        if (dsDanhMucCha.Rows[i]["Ten"].ToString() == "Trang chủ")
                        {
                            link = "../WebSite/Default.aspx";
                        }
                        else
                        {
                            link = "ListNews.aspx?action=cate&chuyenmuc=" + dsDanhMucCha.Rows[i]["Id"].ToString();
                        }
                        //Begin set css active cho menu danh muc duoc chon
                        // Tách chuỗi url và kiểm tra xem đang ở trang chủ hay là  trang con
                        string[] url = Request.Url.LocalPath.ToString().Split('/');
                        string[] chuoiurl = Request.Url.ToString().Split('=');

                        if (dsDanhMucCha.Rows[i]["Ten"].ToString() == "Trang chủ") //Nếu như đang ở trang chủ
                        {
                            // in ra menu cấp 1 với class active mặc định cho trang chủ
                            str += "<li class=\"sm_active\"><div class=\"default_horiz_dropdown_h\"><a class=\"icemenu_link\" href=\"" + link + "\"><span class=\"title\">" + dsDanhMucCha.Rows[i]["Ten"].ToString() + "</span><span class=\"subtitle\"></span><span class=\"icemenu_haschild\"></span></a></div>";
                        }
                        else // Ngược lại nếu đang ở menu # trang chủ
                        {
                            str += "<li class=\"sm_normal\"><div class=\"default_horiz_dropdown_h\"><a class=\"icemenu_link\" href=\"" + link + "\"><span class=\"title\">" + dsDanhMucCha.Rows[i]["Ten"].ToString() + "</span><span class=\"subtitle\"></span><span class=\"icemenu_haschild\"></span></a></div>";                         
                        }
                        str += "<ul id=\"default_364_682_lvl_1\" class=\"default_horiz_dropdown_sm level1\">";
                        for (int k = 0; k < dsDanhMucCon1.Rows.Count; k++)
                        {
                            str += "<li class=\"sm_normal\"><div class=\"default_horiz_dropdown_h\"><a class=\"icemenu_link\" href=\"ListNews.aspx?action=sub&chuyenmuc= " + dsDanhMucCon1.Rows[k]["Id"].ToString() + "\"><span class=\"title\">" + dsDanhMucCon1.Rows[k]["Ten"].ToString() + "</span><span class=\"icemenu_haschild\"></span></a></div></li>";
                        }  
                        //End set css cho menu danh muc duoc chon
                        str += "<li class=\"lastitem\"></li>";
                        str += "</ul>";
                        
                    }
                    str += "</li>";
                }
                str += "</ul>";
            }
            menu.InnerHtml = str;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}